/**
 * @author jmx
 * @description 全局的路由控制文件
 */
import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'

// 引入页面组件
// 登录页面
import Login from '../views/Login'
import Register from '../views/Login/register'
// 主页面
import Home from '../views/Home'
// 404页面
import NotFound from '../views/Common/NotFound'
// 设置页面
import Setting from '../views/Setting'
// 项目界面
import ProjectManagement from '../views/Project'
import ProjectEdit from '../views/Project/edit'
// 采购界面
import PurchaseManagement from '../views/Purchase'
import PurchaseEdit from '../views/Purchase/edit'
import PurchaseDetail from '../views/Purchase/detail'
// 材料界面
import ProductManagement from '../views/Product'
import ProductEdit from '../views/Product/edit'
// 供应商界面
import SupplierManagement from '../views/Supplier'
import SupplierEdit from '../views/Supplier/edit'
// 收获界面
import ReceivingManagement from '../views/Receiving'
import ReceivingEdit from '../views/Receiving/edit'
import ReceivingDetail from '../views/Receiving/detail'
Vue.use(Router)
const router = new Router({
  // 配置模式
  mode: 'history',
  // 配置路由
  routes: [{
    path: '/',
    redirect: '/login'
  }, {
    path: '/login',
    component: Login,
    meta: {
      notLogin: true
    }
  }, {
    path: '/register',
    component: Register,
  }, {
    path: '/home',
    component: Home,
  }, {
    path: '/project/list',
    component: ProjectManagement,
  }, {
    path: '/project/edit',
    name: 'editProject',
    component: ProjectEdit,
  }, {
    path: '/purchase/list',
    component: PurchaseManagement,
  }, {
    path: '/purchase/edit',
    name: 'editPurchase',
    component: PurchaseEdit,
  }, {
    path: '/purchase/detail',
    component: PurchaseDetail,
  }, {
    path: '/product/list',
    component: ProductManagement,
  }, {
    path: '/product/edit',
    name: 'editProduct',
    component: ProductEdit,
  }, {
    path: '/supplier/list',
    component: SupplierManagement,
  }, {
    path: '/supplier/edit',
    name: 'editSupplier',
    component: SupplierEdit,
  }, {
    path: '/setting',
    component: Setting,
  }, {
    path: '/receiving/list',
    component: ReceivingManagement,
  }, {
    path: '/receiving/edit',
    name: 'editReceiving',
    component: ReceivingEdit,
  }, {
    path: '/receiving/detail',
    component: ReceivingDetail,
  }, {
    path: '*',
    component: NotFound,
    meta: {
      notLogin: true
    }
  }]
})
router.beforeEach(function (to, from, next) {
  if (to.meta.notLogin) {
    next()
    return
  }
  const token = store.getters['common/token']
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
